<?php    
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

define('KE', 1);
define('IN_SCRIPT', 1);
define('HESK_PATH', './');
define('HESK_NO_ROBOTS', 1);

/* Get all the required files and functions */
require(HESK_PATH . 'hesk_settings.inc.php');
require(HESK_PATH . 'inc/common.inc.php');
require(HESK_PATH . 'inc/database.inc.php');

hesk_session_start();
if ((isset($_GET['id_product'])) && (isset($_SESSION['id_member']) || isset($_SESSION['id']))) {
    $id_product = isset($_GET['id_product']) ? ($_GET['id_product']): 0;
    $id_user = isset($_SESSION['id_member']) ? ($_SESSION['id_member']): ($_SESSION['id']);
    $user_type = isset($_SESSION['id']) ? 'dokter' : 'user';
    
    //Get Userdata
    hesk_dbConnect();
    if(isset($_SESSION['id'])){
        $sqlMember = hesk_dbQuery("SELECT * FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."member` WHERE id_member='$id_user'");
        $dataMember = hesk_dbFetchAssoc($sqlMember);
        $nama = $dataMember['nama_depan']." ".$dataMember['nama_belakang'];
        $kota = $dataMember['kota'];
        $hp = $dataMember['hp'];
        $emailaddr = $dataMember['email'];
        $perusahaan = $dataMember['perusahaan'];
    } else {
        $sqlMember = hesk_dbQuery("SELECT * FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."users` WHERE id='$id_user'");
        $dataMember = hesk_dbFetchAssoc($sqlMember);
        $nama = $dataMember['nama_depan']." ".$dataMember['nama_belakang'];
        $kota = $dataMember['region'];
        $hp = $dataMember['hp'];
        $emailaddr = $dataMember['email'];
        $perusahaan = $dataMember['tempat_praktek'];
    }
    
    //Get the Name and Link
    $query = "SELECT * FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."promo` WHERE `id`=".hesk_dbEscape($id_product)." LIMIT 1";
    $result = hesk_dbQuery($query);
    $result = hesk_dbFetchAssoc($result);
    $download_link = $result[link];
    $download_name = $result[name];
    
    //Record download log
    $query = "INSERT INTO `".hesk_dbEscape($hesk_settings['db_pfix'])."promo_log` (`id_member` ,`user_type`, `id_promo`) VALUES ('".hesk_dbEscape($id_user)."','". $user_type ."', '".hesk_dbEscape($id_product)."')";
    hesk_dbQuery($query);
    
    /* Format e-mail message for CS */
    $msg = "
    <html>
    <head>
    <title>Informasi dari HelpDesk Support</title>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
    </head>

    <body>
    <p><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Informasi yang 
      masuk dari HelpDesk Support :</font></p>
    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
      <tr> 
        <td width=\"150\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Nama</font></td>
        <td width=\"20\"><div align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">:</font></div></td>
        <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$nama</font></td>
      </tr>
      <tr> 
        <td width=\"150\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Kota</font></td>
        <td width=\"20\"><div align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">:</font></div></td>
        <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$kota</font></td>
      </tr>
      <tr> 
        <td width=\"150\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">HP</font></td>
        <td width=\"20\"><div align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">:</font></div></td>
        <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$hp</font></td>
      </tr>
      <tr> 
        <td width=\"150\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Email</font></td>
        <td width=\"20\"><div align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">:</font></div></td>
        <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$emailaddr</font></td>
      </tr>
      <tr> 
        <td width=\"150\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Perusahaan</font></td>
        <td width=\"20\"><div align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">:</font></div></td>
        <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$perusahaan</font></td>
      </tr>
      <tr> 
        <td width=\"150\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Produk Download</font></td>
        <td width=\"20\"><div align=\"center\"><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">:</font></div></td>
        <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$download_name</font></td>
      </tr>
    </table>
    <p>&nbsp;</p>
    </body>
    </html>
    ";

    /* Send e-mail */
    $email = $hesk_settings['support_mail'];
    $headers = "From: Pengunjung Web <$hesk_settings[noreply_mail]>\n";
    $headers.= "Reply-to: $hesk_settings[noreply_mail]\n";
    $headers.= "Return-Path: $hesk_settings[webmaster_mail]\n";
    $headers.= "Content-type: text/html; charset=".$hesklang['ENCODING'];
    @mail($email,"Download SimpleBiz Trial",$msg,$headers);
    
    //Download and redirect
    header("Location: ".$download_link);
} else {
    hesk_process_messages('Download failed', './index.php');
}

?>
